//
//  HomeCells.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/2/6.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import UIKit
import SnapKit
import AlamofireImage

// MARK:-
// MARK: 模块Cell
class HomeModuleCell: UICollectionViewCell {
    
    static func identifier() -> String {
        return NSStringFromClass(self.classForCoder())
    }
    
    var label:UILabel!
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func set(title:String?, index:Int) {
        label.text = title

        var rgb: Int
        
        switch index % 6 {
        case 0:
            rgb = 0xFBD4D4

        case 1:
            rgb = 0xCDE0F5
            
        case 2:
            rgb = 0xFFE5BA
            
        case 3:
            rgb = 0xA6EDE1
            
        case 4:
            rgb = 0xFFD1B1

        default:
            rgb = 0xE9DAF8
        }
        layer.borderColor = UIColor.lsf_color16(rgb).cgColor
        layer.borderWidth = 4
    }
    
    
    public func set(title:String?) {
        label.text = title
        
        layer.borderColor = UIColor.lsf_color16(0xD3D3D3).cgColor
        layer.borderWidth = 1
    }
    
    func setup() {
        
        backgroundColor = UIColor.clear
        layer.cornerRadius = 4
        clipsToBounds = true
        layer.borderWidth = 1
        
        label = UILabel.init()
        label.textAlignment = .center
        label.textColor = UIColor.black
        label.font = UIFont.systemFont(ofSize: 14)
        
        addSubview(label)
        
        label.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0))
        }
        
        
    }
}

// MARK:-
// MARK: 考试倒计时
class HomeExamInfoCell: HomeModuleCell {
    
    
    var subLabel:UILabel!

    
    func set(title:String?, subTitle:String?) {
        label.text = title
        subLabel.text = subTitle
        layer.borderColor = UIColor.lsf_color16(0xD3D3D3).cgColor
    }
    
    override func setup() {
        super.setup()
        
        
        label.font = UIFont.systemFont(ofSize: 17)
        
        subLabel = UILabel.init()
        subLabel.textAlignment = .center
        subLabel.textColor = UIColor.black
        subLabel.font = UIFont.systemFont(ofSize: 14)
        addSubview(subLabel)
        
        label.snp.remakeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(12)
        }
        
        subLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(label.snp.bottom).offset(5)
        }
    }

}

// MARK:-
// MARK: 头条新闻列表
class HomeNewsCell: UICollectionViewCell {
    
    static func identifier() -> String {
        return NSStringFromClass(self.classForCoder())
    }
    
    var titleLabel:UILabel!
    var imageView:UIImageView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setup()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func set(title:String?, imageUrl:String?) {
        titleLabel.text = title
        
        guard let urlStr = imageUrl else { return }
        guard let url = URL.init(string: urlStr) else { return }
        imageView.af_setImage(withURL: url)
    }
    
    func setup() {
        
        titleLabel = UILabel.init()
        titleLabel.textAlignment = .left
        titleLabel.textColor = UIColor.black
        titleLabel.numberOfLines = 0
        titleLabel.font = UIFont.systemFont(ofSize: 14)
        addSubview(titleLabel)
        
        imageView = UIImageView.init()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        addSubview(imageView)
        
        
        let bottomLine = UIView()
        bottomLine.backgroundColor = UIColor.lsf_color16(0xD3D3D3)
        addSubview(bottomLine)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(16)
            make.leading.equalTo(10)
            make.bottom.lessThanOrEqualTo(-16)
        }
        
        imageView.snp.makeConstraints { (make) in
            make.trailing.equalTo(-8)
            make.leading.equalTo(titleLabel.snp.trailing).offset(11)
            make.size.equalTo(CGSize.init(width: 128, height: 72))
            make.centerY.equalToSuperview()
        }
        
        bottomLine.snp.makeConstraints { (make) in
            make.bottom.trailing.equalTo(0)
            make.leading.equalTo(12)
            make.height.equalTo(0.5)
        }
    }
}

// MARK:-
// MARK:头条标题
class HomeNewsHeaderView: UICollectionReusableView {
    
    static func identifier() -> String {
        return NSStringFromClass(self.classForCoder())
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let label = UILabel.init()
        label.text = "头条"
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.black
        addSubview(label)
        label.snp.makeConstraints({ (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(10)
        })
        
        let topLine = UIView()
        topLine.backgroundColor = UIColor.lsf_color16(0xD3D3D3)
        addSubview(topLine)
        
        let bottomLine = UIView()
        bottomLine.backgroundColor = UIColor.lsf_color16(0xD3D3D3)
        addSubview(bottomLine)
        
        topLine.snp.makeConstraints { (make) in
            make.leading.top.trailing.equalTo(0)
            make.height.equalTo(0.5)
        }
        
        bottomLine.snp.makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(0)
            make.height.equalTo(0.5)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
